package com.woniuxy.classdeans.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.classdeans.exception.ClassdeansException;
import com.woniuxy.classdeans.service.TeacherInfoService;
import com.woniuxy.commons.entity.ResponseResult;
import com.woniuxy.commons.entity.wyh.ResponseTable;
import com.woniuxy.commons.entity.wyh.TeacherInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 老师信息
 *
 * @author 王艳辉
 * @date 2020/7/16 - 20:21
 */
@RestController
@RequestMapping("teacher")
@Transactional
@Slf4j
public class TeacherInfoController {
    @Autowired
    private TeacherInfoService teacherInfoService;



    /**
     * 分页获取老师信息
     * @return
     */
    @RequestMapping("getall")
    public Object getall() {
        //返回消息类型,默认失败
        ResponseTable<TeacherInfo> res = new ResponseTable<TeacherInfo>().setCode(400).setMsg("获取老师信息失败！");
        try {
            //获取所有老师信息
            List<TeacherInfo> all = teacherInfoService.getAll();
            if (res != null) {
                //数据返回给前端
                res.setCode(0).setMsg("获取老师信息成功！").setData(all).setCount(all.size());
            }
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("teacher/getall异常");
        }


        return res;
    }


    /**
     * 分页获取老师信息
     * @param page 页码
     * @param limit 限制
     * @return
     */
    @RequestMapping("getallbypage")
    public Object getallbypage(Integer page, Integer limit) {
        //返回消息类型,默认失败
        ResponseTable<TeacherInfo> res = new ResponseTable<TeacherInfo>().setCode(400).setMsg("获取老师信息失败！");

        try {
            PageHelper.startPage(page, limit);//分页插件

            //获取所有老师信息
            List<TeacherInfo> all = teacherInfoService.getallbypage();

            //System.out.println(all);//打印信息为分页信息page{....}
            PageInfo<TeacherInfo> pages = new PageInfo<>(all);//转换为分页对象可获取分页信息
            if (all != null) {

                //数据返回给前端
                res.setCode(0).setMsg("获取老师信息成功！").setData(all).setCount((int) pages.getTotal());
            }
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("teacher/getallbypage异常");
        }


        return res;
    }

    /**
     * 修改老师信息
     * @param teacherInfo
     * @return
     */
    @RequestMapping("updateTeacherInfo")
    public Object updateTeacherInfo(TeacherInfo teacherInfo){

        System.out.println("接受到对象信息为："+teacherInfo);
        //默认返回false
        ResponseResult<TeacherInfo> res =
                new ResponseResult<TeacherInfo>().setMessage("更新教师信息失败").setCode(400);

        try {
            //更新当前的用户信息
            if(teacherInfo.getTcId()!=null){
                //更新教师信息
                int num = teacherInfoService.updateTeacherInfo(teacherInfo);

                //如果更新成功，返回更新后的教师信息
                if(num==1){
                    //获取最新的教师信息返回
                    TeacherInfo latest= teacherInfoService.getByTcid(teacherInfo.getTcId());
                    res.setData(latest).setCode(200).setMessage("更新教师数据成功！");
                }
            }
        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("teacher/updateTeacherInfo异常");
        }


        return res;
    }

    /**
     * 重置老师密码信息
     * @param teacherInfo 老师id
     * @return
     */
    @RequestMapping("resetPassword")
    public Object resetPassword(TeacherInfo teacherInfo){
        System.out.println("接受到对象信息为："+teacherInfo);
        //默认返回false
        ResponseResult<TeacherInfo> res =
                new ResponseResult<TeacherInfo>().setMessage("重置密码失败").setCode(400);

        try {
            //重置密码
            if(teacherInfo.getTcId()!=null){
                //重置密码
                int num = teacherInfoService.resetPassword(teacherInfo);

                //如果重置成功，返回更新后密码
                if(num==1){
                    //返回默认的密码
                    teacherInfo.setPassword("123456");
                    res.setData(teacherInfo).setCode(200).setMessage("重置密码成功！");
                }
            }

        }catch (Exception e) {
            log.error(e.getMessage());//日志记录
            //抛自定义异常
            throw new ClassdeansException("teacher/resetPassword异常");
        }
        return res;
    }
}
